package io.renren.modules.xk.controller;


import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.xk.entity.XkCourseEntity;
import io.renren.modules.xk.entity.XkTaskEntity;
import io.renren.modules.xk.service.XkCourseService;
import io.renren.modules.xk.service.XkTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 选课信息管理、发布
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/xk/task")
public class XkTaskController {
	@Autowired
	private XkTaskService xkTaskService;

	/**
	 * 所有任务列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("xk:task:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = xkTaskService.queryPage(params);
		return R.ok().put("page", page);
	}
	
	/**
	 * 任务信息
	 */
	@RequestMapping("/info/{taskId}")
	@RequiresPermissions("xk:task:info")
	public R info(@PathVariable("taskId") Long taskId){
		XkTaskEntity taskEntity = xkTaskService.getById(taskId);
		return R.ok().put("task", taskEntity);
	}
	
	/**
	 * 保存任务
	 */
	@SysLog("保存任务")
	@RequestMapping("/save")
	@RequiresPermissions("xk:task:save")
	public R save(@RequestBody XkTaskEntity taskEntity){
		ValidatorUtils.validateEntity(taskEntity, AddGroup.class);
		taskEntity.setStatus("0");
		xkTaskService.saveEntity(taskEntity);
		return R.ok();
	}
	
	/**
	 * 修改任务
	 */
	@SysLog("修改任务")
	@RequestMapping("/update")
	@RequiresPermissions("xk:task:update")
	public R update(@RequestBody XkTaskEntity taskEntity){
		ValidatorUtils.validateEntity(taskEntity, UpdateGroup.class);
		xkTaskService.update(taskEntity);
		return R.ok();
	}
	
	/**
	 * 删除任务
	 */
	@SysLog("删除任务")
	@RequestMapping("/delete")
	@RequiresPermissions("xk:task:delete")
	public R delete(@RequestBody Long[] taskIds){
		xkTaskService.removeByIds(Arrays.asList(taskIds));
		return R.ok();
	}
	/**
	 * 发布任务
	 */
	@SysLog("发布任务")
	@RequestMapping("/release")
	@RequiresPermissions("xk:task:release")
	public R release(@RequestBody XkTaskEntity taskEntity){
		xkTaskService.taskRelease(taskEntity);
		return R.ok();
	}
}
